<!--
 * @Author: thats-all-right-ha-ha 8280209+thats-all-right-ha-ha@user.noreply.gitee.com
 * @Date: 2024-02-21 16:57:55
 * @LastEditors: thats-all-right-ha-ha 8280209+thats-all-right-ha-ha@user.noreply.gitee.com
 * @LastEditTime: 2024-02-26 20:02:32
 * @FilePath: \hrManage\src\views\department\index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="container">
    <div class="app-container">
      <el-tree :expand-on-click-node="false" :data="data" :props="defaultProps" :default-expand-all="true">
        <template v-slot="{ data }">
          <el-row style="width: 100%; height: 40px" type="flex" justify="space-around" align="middle">
            <el-col>{{ data.name }}</el-col>
            <el-col>
              <span class="manage">{{ data.managerName }}</span>
              <el-dropdown @command="handleCommand($event,data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加部门</el-dropdown-item>
                  <el-dropdown-item command="update">修改部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除部门</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>

    </div>
    <add-dept ref="addDept" :show-dialog.sync="dialogFormVisible" :child-id="childId" @Department="getDepartment" />
  </div>
</template>
<script>
import { delDepartment, getDepartment } from '@/api/department'
import { getDepartmentTree } from '@/utils/index'
import AddDept from './components/add-dept.vue'
export default {
  name: 'Department',
  components: {
    AddDept
  },
  data() {
    return {
      data: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      dialogFormVisible: false,
      childId: undefined
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    async getDepartment() {
      const res = await getDepartment()
      this.data = getDepartmentTree(res, 0)
    },
    handleCommand(command, id) {
      if (command === 'add') {
        this.dialogFormVisible = true
        this.childId = id
      }
      if (command === 'update') {
        this.childId = id
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs.addDept.update()
        })
      }
      if (command === 'del') {
        this.$confirm('是否删除该部门').then(async() => {
          await delDepartment(id)
          this.getDepartment()
        })
      }
    }
  }
}
</script>
<style scoped>
  .manage{
    display: inline-block;
    margin-right: 10px;
  }
</style>
